Add support for context and comments on properties
authorMatthias Clasen <matthiasc@src.gnome.org>
Sun, 8 Jul 2007 05:27:22 +0000 (05:27 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Sun, 8 Jul 2007 05:27:22 +0000 (05:27 +0000)
svn path=/trunk/; revision=18401

ChangeLog
docs/reference/ChangeLog
docs/reference/gtk/tmpl/gtkbuilder.sgml
gtk/gtkbuilderparser.c
gtk/gtkbuilderprivate.h

index de7971429e2ce9d459d67cb678ae7db637bcc41b..2d22507448af0ccc5c5c8b404ca83ab12628b10d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2007-07-07  Matthias Clasen <mclasen@redhat.com>
+
+       * gtk/gtkbuilderprivate.h:
+       * gtk/gtkbuilderparser.c: Support context and comments
+       for properties.
+
 2007-07-07  Matthias Clasen <mclasen@redhat.com>
 
        * gtk/gtkwidget.c: Fix some typos, and a memory management bug.  
index 3a649df2932a75f2ed3ddeaf024933ba0e6a07b3..7325eda066df5273068de1b18f5c40e1460ae196 100644 (file)
@@ -1,3 +1,8 @@
+2007-07-07  Matthias Clasen  <mclasen@redhat.com>
+
+       * gtk/tmpl/gtkbuilder.sgml: Document context and
+       comments for properties.
+
 2007-07-07  Johan Dahlin  <jdahlin@async.com.br>
 
        * gtk/gtk-builder-convert.xml: Update
index f9a45d74d13bb2829098ee81722cf9528c4cd28e..1a83eb3cb067839a9dcc6fece5aacb2fa3b0bd2b 100644 (file)
@@ -62,7 +62,9 @@ which are more limited in scope.
                      type-func      #IMPLIED
                      constructor    #IMPLIED >
 <!ATTLIST property   name           #REQUIRED
-                     translatable   #IMPLIED >
+                     translatable   #IMPLIED 
+                     comments       #IMPLIED
+                     context        #IMPLIED >
 <!ATTLIST signal     name           #REQUIRED
                      handler        #REQUIRED
                      after          #IMPLIED
@@ -114,7 +116,9 @@ set to a true value, GTK+ uses gettext() (or dgettext() if
 the builder has a translation domain set) to find a translation 
 for the value. This happens before the value is parsed, so
 it can be used for properties of any type, but it is probably
-most useful for string properties.
+most useful for string properties. It is also possible to
+specify a context to disambiguate short strings, and comments
+which may help the translators.
 </para>
 <para>
 GtkBuilder can parse textual representations for the most
index 2996ac8a4ffafab8fd6450b2493e465cb4973ff9..d7605fea1eba9141f2cf8583a5299d64a15dc220 100644 (file)
@@ -393,6 +393,7 @@ parse_property (ParserData   *data,
 {
   PropertyInfo *info;
   gchar *name = NULL;
+  gchar *context = NULL;
   gboolean translatable = FALSE;
   int i;
 
@@ -408,6 +409,14 @@ parse_property (ParserData   *data,
                                                 error))
            return;
        }
+      else if (strcmp (names[i], "comments") == 0)
+        {
+          /* do nothing, comments are for translators */
+        }
+      else if (strcmp (names[i], "context") == 0) 
+        {
+          context = g_strdup (values[i]);
+        }
       else
        {
          error_invalid_attribute (data, element_name, names[i], error);
@@ -424,6 +433,7 @@ parse_property (ParserData   *data,
   info = g_slice_new0 (PropertyInfo);
   info->name = name;
   info->translatable = translatable;
+  info->context = context;
   state_push (data, info);
 
   info->tag.name = element_name;
@@ -833,6 +843,31 @@ end_element (GMarkupParseContext *context,
     }
 }
 
+/* This function is taken from gettext.h 
+ * GNU gettext uses '\004' to separate context and msgid in .mo files.
+ */
+static const char *
+dpgettext (const char *domain,
+           const char *msgctxt,
+           const char *msgid)
+{
+  size_t msgctxt_len = strlen (msgctxt) + 1;
+  size_t msgid_len = strlen (msgid) + 1;
+  const char *translation;
+  char msg_ctxt_id[msgctxt_len + msgid_len];
+
+  memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
+  msg_ctxt_id[msgctxt_len - 1] = '\004';
+  memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
+
+  translation = dgettext (domain, msg_ctxt_id);
+
+  if (translation != msg_ctxt_id) 
+    return translation;
+  return msgid;
+}
+
 /* Called for character data */
 /* text is not nul-terminated */
 static void
@@ -865,10 +900,10 @@ text (GMarkupParseContext *context,
 
       if (prop_info->translatable && text_len)
         {
-          if (data->domain)
-            text = dgettext (data->domain, text);
+          if (prop_info->context)
+            text = dpgettext (data->domain, prop_info->context, text);
           else
-            text = gettext (text);
+            text = dgettext (data->domain, text);
         }
       prop_info->data = g_strndup (text, text_len);
     }
index 70c9f7941f8c37e85eb7166e502159418e7ebc54..1c3f18e68a6fd493509095bd8c5a27bc24883c38 100644 (file)
@@ -61,6 +61,7 @@ typedef struct {
   gchar *name;
   gchar *data;
   gboolean translatable;
+  gchar *context;
 } PropertyInfo;
 
 typedef struct {